<?php

namespace app\admin\controller\banner;

use app\common\controller\Backend;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Buy extends Backend
{

    /**
     * Buy模型对象
     * @var \app\common\model\banner\Buy
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\banner\Buy;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    // 通过审核
    public function examine($ids)
    {
        $result =  $this->model->where('id', $ids)->find();
        $end_time = $result['long'] * 86400 +time();

        $this->model->where('id', $ids)->update(['status'=>'1','end_time'=>$end_time]);

        $this->success('成功');
    }
    // 不通过审核
    public function failed($ids)
    {

        $result = $this->model->where('id', $ids)->find();
        if ($result['type']==1)
        {
            Db::name('banner')->where('id',$result['banner_id'])->update(['type'=>0,'user_id'=>0,'buy_time'=>0,'banner_price_id'=>0]);
        }elseif ($result['type']==2)
        {
            Db::name('banner_shop')->where('id',$result['banner_id'])->update(['type'=>0,'user_id'=>0,'buy_time'=>0,'banner_price_id'=>0]);
        }elseif ($result['type']==3)
        {
            Db::name('banners')->where('id',$result['banner_id'])->update(['type'=>0,'user_id'=>0,'buy_time'=>0,'banner_price_id'=>0]);
        }

        $this->model->where('id', $ids)->update(['status'=>'2']);
        $this->success('成功');
    }
    /**
     * 删除
     *
     * @param $ids
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $list = $this->model->where($pk, 'in', $ids)->select();
        $image = Db::name('config')->where('id',39)->value('value');
        $count = 0;
        Db::startTrans();
        try {
            foreach ($list as $item) {

                if ($item['type']==1)
                {
                    Db::name('banner')->where('id',$item['banner_id'])->update(['type'=>0,'user_id'=>0,'type_url'=>3,'buy_time'=>0,'banner_price_id'=>0,'ex_status'=>1,'image'=>$image]);
                }elseif ($item['type']==2)
                {
                    Db::name('banner_shop')->where('id',$item['banner_id'])->update(['type'=>0,'user_id'=>0,'type_url'=>3,'buy_time'=>0,'banner_price_id'=>0,'ex_status'=>1,'image'=>$image]);
                }elseif ($item['type']==3)
                {
                    Db::name('banners')->where('id',$item['banner_id'])->update(['type'=>0,'user_id'=>0,'type_url'=>3,'buy_time'=>0,'banner_price_id'=>0,'ex_status'=>1,'image'=>$image]);
                }

                $count += $item->delete();
            }
            Db::commit();
        } catch (PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success();
        }
        $this->error(__('No rows were deleted'));
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->nickname = Db::name('user')->where('id',$row->user_id)->value('nickname');
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
